package com.itheima.reggie.controller;

import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物项
     */
    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //1. 先查询ShoppingCart是否存在
        // select * from where  user_id=? and ( dish=? or setmeal_id=? )
        ShoppingCart cart = shoppingCartService.query()
                .eq("user_id", BaseContext.get())
                .eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId())
                .one();
        //2. 如果存在，将number加1
        if (cart != null) {
            ///number数量加1
            cart.setNumber(cart.getNumber() + 1);
            //更新操作
            shoppingCartService.updateById(cart);
            return R.success(cart);
        } else {
            //3. 如果不存在，就添加操作
            //补全数据
            shoppingCart.setUserId(BaseContext.get());//设置用户id
            shoppingCart.setNumber(1); //设置数量
            shoppingCart.setCreateTime(LocalDateTime.now()); //设置时间
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }


    /**
     * 查询用户购物车
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        //根据用户id查询  select * from shopping_cat where user_id=?
        List<ShoppingCart> list = shoppingCartService.query()
                .eq("user_id", BaseContext.get())
                .orderByDesc("create_time")
                .list();
        return R.success(list);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("clean")
    public R<String> clean() {
        //根据用户id删除
        shoppingCartService.update()
                .eq("user_id", BaseContext.get())
                .remove();
        return R.success("清空成功");
    }

    /**
     * 添加/删除购物项
     */
    @PostMapping("sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //1. 根据用户id、提交dishId|setmealId，查询菜品|套餐，判断购物车商品的数量
        //select * from shopping_cart where user_id=? and dish_id=?
        ShoppingCart cart = shoppingCartService.query()
                .eq("user_id", BaseContext.get())
                .eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId())
                .one();
        if (cart.getNumber() > 1) {// 更新数据，减减
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartService.updateById(cart);
        } else {// 删除数据 delete from shopping_cart where id=?
            shoppingCartService.removeById(cart.getId());
            cart.setNumber(0);//针对前端特殊处理
        }
        return R.success(cart);
    }
}
